package com.example.usermanager.controller;


import com.example.usermanager.mapper.UserMapper;
import com.example.usermanager.model.UserInfo;
import com.example.usermanager.tools.AppConfig;
import com.example.usermanager.tools.ResponseBody;
import com.example.usermanager.tools.SessionUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/login")
    public ResponseBody<UserInfo> login(@RequestParam String username,
                                        @RequestParam String password,
                                        HttpServletRequest request){
        UserInfo userInfo = userMapper.login(username,password); //查询的结果
        int status = -1;
        String message = "出现了未知的错误！";
        if(userInfo != null && userInfo.getId() > 0){
            status = 0;
            message = "";
            //将用户对象存储到session中!!!
            HttpSession session = request.getSession();
            session.setAttribute(AppConfig.USERINFO_SESSION_KEY,userInfo);
        }
        return new ResponseBody<>(status,message,userInfo);
        //return userMapper.login(username,password);
    }

    /**
     * 考虑接口的返回值是什么呢
     */


    //查询当前用户的权限，不传递参数，可以在session中获取用户id，传递request为了获得session
    @RequestMapping("/checkadmin")
    public ResponseBody<Integer> checkAdmin(HttpServletRequest request){
        int data = 0;
        HttpSession session = request.getSession(false); //没有session则默认不创建session，因此设置false
        UserInfo userInfo = null;
        if(session != null &&  (userInfo = (UserInfo)session.getAttribute(AppConfig.USERINFO_SESSION_KEY)) != null){
            data = userInfo.getIsadmin();
        }
        return new ResponseBody<>(0,"",data);
    }

    @RequestMapping("/add")
    //前端将传递过来的参数封装成一个对象，传递过来的参数要与UserInfo属性对应
    public ResponseBody<Integer> add(UserInfo userInfo, HttpServletRequest request){
        int status = 0;
        String message = "";
        int data = 0;
        //安全效验
        UserInfo user = SessionUtil.getUserBySession(request);
        if(user == null){
            //表示未登录状态
            status = -1;
            message = "当前用户未登录，请先登录！";
        }else if(userInfo.getIsadmin() == 1){
            if(user.getIsadmin() == 0){
                status = -2;
                message = "当前用户权限不足，请先登录";
            }else if(user.getIsadmin() == 1){
                //当前用户为超级管理员
                data = userMapper.add(userInfo);
                status = 0;
            }
        }else if(user.getIsadmin() == 0){
            data = userMapper.add(userInfo);  //添加一个普通管理员
        }
        return new ResponseBody<>(status,message,data);
    }

    @RequestMapping("/getuser")
    public ResponseBody<UserInfo> getUser(@RequestParam int uid){
        int status = -1;
        String message = "出现了未知的错误！";
        UserInfo userInfo = userMapper.getUser(uid);
        if(userInfo != null){
            //TODO 权限效验
            status = 0;
            message = "";
        }
        return new ResponseBody<>(status,message,userInfo);
    }


    @RequestMapping("/update")
    public ResponseBody<Integer> update(UserInfo userInfo){
        int data = 0;
        //TODO 权限效验
        data = userMapper.update(userInfo);
        return new ResponseBody<>(0,"",data);
    }


    //获取列表详情
    @RequestMapping("/list")
    public ResponseBody<HashMap<String,Object>> getList(String name,
                                                        String address,
                                                        String email,
                                                        int cpage,
                                                        int psize,
                                                        HttpServletRequest request){
        //判断用户是否登录,在删除、增加也要做
        UserInfo userInfo = SessionUtil.getUserBySession(request);
        if(userInfo == null){
            return new ResponseBody<>(-1,"当前用户未登录", null);
        }

        Integer isadmin = null;
        //用户等级判断
        if(userInfo.getIsadmin() == 0){
            isadmin = 0;
        }
        //处理前端传递过来的参数
        name = name.equals("") ? null : name;
        address = address.equals("") ? null : address;
        email = email.equals("") ? null : email;

        //构建分页查询的字段，查询当前页面的列表信息
        int skipCount = (cpage-1)*psize; //跳过查询的条数

        List<UserInfo> list = userMapper.getListByPage(name,address,email,skipCount,psize,isadmin);
        //查询满足条件的数据总条数
        int tcount = userMapper.getCount(name,address,email,isadmin);
        //总页数......
        int tpage = (int)Math.ceil(tcount/(psize*1.0));

        HashMap<String,Object> data = new HashMap<>();

        data.put("list",list);
        data.put("tcount",tcount);
        data.put("tpage",tpage);

        return new ResponseBody<>(0,"",data);
    }

    @RequestMapping("/delete")
    public ResponseBody<Integer> del(@RequestParam int id, HttpServletRequest request){
        //先进行用户权限效验
        UserInfo userInfo = SessionUtil.getUserBySession(request);
        if(userInfo == null){
            //表明用户未登录
            return new ResponseBody<>(-1,"用户未登录",0);
        }
        //判断删除的是否是自己
        if(id == userInfo.getId()){
            return new ResponseBody<>(-2,"不能删除自己",0);
        }
        //用户等级
        Integer isadmin = null;
        if(userInfo.getIsadmin() == 0){
            isadmin = 0;
        }
        int result = userMapper.del(id,isadmin);
        return new ResponseBody<>(0,"",result);
    }

    @RequestMapping("/delmore")
    public ResponseBody<Integer> delMore(String ids){
        int result = 0;
        result = userMapper.delMore(ids.split(","));
        return new ResponseBody<>(0,"",result);
    }
}
